<template>
    <div >
      <zh-row>
        <zh-col :span="4">
          <FileCollectTree ref="classTreeRef" @nodeClick="treeNodeClick" style="height:800px">
          </FileCollectTree>
        </zh-col>
        <zh-col :span="20">
          <zh-card title="">
            <zh-form :model="state.queryParams" ref="queryFormRef" label-width="150px">
              <zh-form-select
                  prop="upAppId"
                  :lazy="true"
                  label="所属应用"
                  v-model="state.queryParams.upAppId"
                  :fieldNames="{ label: 'appName', value: 'appId' }"
                  funcCode="application0012"
                  searchField="appName"
              ></zh-form-select>
              <zh-form-select
                  prop="secrecyLevel"
                  label="保密级别"
                  :options="state.secrecyLevelDict"
                  v-model="state.queryParams.secrecyLevel"
              ></zh-form-select>
              <zh-form-select
                  prop="fieldId"
                  :lazy="true"
                  label="元数据"
                  v-model="state.queryParams.fieldId"
                  :fieldNames="{ label: 'fieldName', value: 'fieldId' }"
                  :url="VITE_FILE_API_URL+'fileFieldParam0001'"
                  searchField="fieldName"
              ></zh-form-select>
              <zh-form-input prop="fieldValue" label="文件元数据值"  v-model="state.queryParams.fieldValue"
                            ></zh-form-input>
              <zh-form-query v-model="state.advanced"  :form-ref="queryFormRef" @queryClick="queryCollectData"
                             :table-ref="fileCollectDataTable">
                             <zh-button buttonType="query" @click="queryDetail">查看详情</zh-button>
                             <zh-button buttonType="edit" @click="editDetail">修改</zh-button>
                             <zh-button buttonType="add" @click="childClassAdd">子级新增</zh-button>
                             <zh-button buttonType="save" @click="baseClassAdd">根目录新增</zh-button>
                             <zh-button buttonType="del" @click="delClass">删除分类</zh-button>
              </zh-form-query>
                  
            </zh-form>
          </zh-card>
          <zh-table
              ref="fileCollectDataTable"
              title="文件列表"
              :height="600"
              :url="VITE_FILE_API_URL+'fileCollectData0001'"
              :columns="state.tableColumn"
              :disabled="false"
              highlight-current-row
          >
          <template #secrecyLevel="{row}">
              <zh-select
                  :disabled="true"
                  prop="secrecyLevel"
                  label="保密级别"
                  :options="state.secrecyLevelDict"
                  v-model="row.secrecyLevel"
              ></zh-select>
            </template>
            <template #encryptionFlag="{row}">
              <zh-select
                  :disabled="true"
                  prop="encryptionFlag"
                  label="是否加密"
                  :options="state.dictData.flag"
                  v-model="row.encryptionFlag"
              ></zh-select>
            </template>
            <template #fielderFlag="{row}">
              <zh-select
                  :disabled="true"
                  prop="fielderFlag"
                  label="是否归档"
                  :options="state.dictData.flag"
                  v-model="row.fielderFlag"
              ></zh-select>
            </template> 
            <template #op="{row}">
              <el-tooltip content="元数据" placement="top">
                <el-button :icon="Link" link @click="link(row)"></el-button>
              </el-tooltip>
              <el-tooltip content="编辑" placement="top">
                <el-button :icon="Edit" link @click="edit(row)"></el-button>
              </el-tooltip>
              <el-tooltip content="下载" placement="top">
                <el-button :icon="Download" link @click="download(row)"></el-button>
              </el-tooltip>
            </template>
          </zh-table>
        </zh-col>
      </zh-row>
      

      <zh-dialog style="width:85%" :title=state.modalTitle v-model="state.visible">
            <FileCollectDataForm  ref="fileCollectDataPage" :show-flag="state.showFlag" :FileCollectData="state.selectRow" v-model="state.visible"></FileCollectDataForm>
            <template #footer>
                <span class="dialog-footer">

                    <zh-button @click="state.visible = false">取消</zh-button>
                    <zh-button v-if="!state.showFlagSave" type="primary" @click="addFileCollectData">
                        保存
                    </zh-button>
                </span>
            </template>
       </zh-dialog>
       <zh-dialog
          ref="fileInfoDialog"
          v-model="state.fileInfoVisible"
          title="文件信息"
          width="80%"
          @confirm="saveFileInfo">
          <file-info ref="fileInfoRef" :disabled="false"></file-info>
      </zh-dialog>
      <el-dialog
          ref="fileMetaDataDialog"
          v-model="state.fileMetaDataVisible"
          title="文件元数据"
          width="80%"
        >
          <file-meta-data ref="fileMetaDataRef" :disabled="false"></file-meta-data>
      </el-dialog>
    </div>
</template>

<script lang="ts" setup name="fileCollectDataIndex">
import FileCollectTree from '@/views/fileCenter/fileCollectClass/component/FileCollectTree.vue';
import FileCollectDataForm from '@/views/fileCenter/fileCollectData/component/FileCollectDataForm.vue';
import {Edit,Link,Download} from '@element-plus/icons-vue'
import FileInfo from '@/views/fileCenter/fileManager/component/fileInfo.vue';
import FileMetaData from '@/views/fileCenter/fileManager/component/fileMetaData.vue';
const VITE_FILE_API_URL = import.meta.env.VITE_FILE_API_URL as string+'/doc/api/';


const classTreeRef=ref();
const queryFormRef = ref<any>({});
const fileCollectDataTable = ref<any>({});
const fileCollectDataPage = ref();
const fileMetaDataRef=ref();
const fileInfoRef=ref();

const state = reactive({
    modalTitle:"操作",
    visible:false,
    showFlag:false,
    showFlagSave:false,
    selectRow:{},
    advanced:true,
    dictData:[] as any,
    fileInfoVisible:false,
    fileMetaDataVisible:false,
    queryParams:{
        fileClassCode:'',
        upAppId:'',
        secrecyLevel:'',
        fieldId:'',
        fieldValue:'',

    },
    secrecyLevelDict:[
        {
        dictKey:'1',
        dictValue:'一级'
        },
        {
        dictKey:'2',
        dictValue:'二级'
        },
        {
        dictKey:'3',
        dictValue:'三级'
        },
    ],
  tableColumn:[
    {type: 'seq', width: 80, fixed: 'left', align: 'center', title: '序号'},
    {field: 'collectDataId', title: 'collectDataId', fixed: 'left', visible: false, align: 'center', width: 150},
    {field: 'fileId', title: '文件id', fixed: 'left', visible: false, align: 'center', width: 150},
    {field: 'fileClassCode', title: '文件分类',visible: false, fixed: 'left', align: 'center', width: 150},
    {field: 'classTitle', title: '文件分类', fixed: 'left', align: 'center', width: 150},
    {field: 'fileName', title: '文件名称',  fixed: 'left',align: 'center', minWidth: 200},
    {field: 'showId', title: '文件显示id',  align: 'center', minWidth: 150},
    {field: 'fileSuffix', title: '文件后缀（文件类型）',  align: 'center', minWidth: 200},
    {field: 'fileEnvId', title: '文件环境主键',visible:false,  align: 'center', minWidth: 200},    
    {field: 'collectTitle', title: '归档标题',  align: 'center', minWidth: 100},
    {field: 'secrecyLevel', title: '保密级别',  align: 'center', minWidth: 200,slots: {default: 'secrecyLevel'}},
    {field: 'filePath', title: '文件存放路径',  align: 'center', minWidth: 200},
    {field: 'fileSize', title: '文件大小',  align: 'center', minWidth: 200},    
    {field: 'encryptionFlag', title: '是否加密',  align: 'center', minWidth: 200,slots: {default: 'encryptionFlag'}},
    {field: 'filePassowrd', title: '文件加密密码',  align: 'center', minWidth: 200},
    {field: 'zipPassword', title: '压缩包解压密码',  align: 'center', minWidth: 200},
    {field: 'fielderFlag', title: '是否归档',  align: 'center', minWidth: 200,slots: {default: 'fielderFlag'}},
    {field: 'upAppName', title: '上传应用',  align: 'center', minWidth: 200},
    {field: 'uploader', title: '上传人',  align: 'center', minWidth: 200},
    {field: 'uploaderId', title: '上传人id', visible:false, align: 'center', minWidth: 200},
    {field: 'fileder', title: '归档人',  align: 'center', minWidth: 200},
    {field: 'filederId', title: '归档人id', visible:false, align: 'center', minWidth: 200},
    {field: 'filederTime', title: '归档时间',  align: 'center', minWidth: 200},
    {field: 'remarks', title: '备注',  align: 'center', minWidth: 200},
    {field: 'fileMd5', title: '文件md5',  align: 'center', minWidth: 200},
    {field: 'fileClassPath', title: '文件存放路径',  align: 'center', minWidth: 200},
    {field: 'collectDataCode', title: '归档编码',  align: 'center', minWidth: 200},
    
    {field: 'op', title: '操作',  align: 'center',fixed:'right',width:200,slots: {default: 'op'}},
    ]

});


const params = {
  dictFields: 'flag',
};
$api.common.getBatchDict.post(params).then((res) => {
  state.dictData = res.result;
});

//--------------------------定义调用方法--------------------------------------------------
//获取当前节点赋值查询条件
function treeNodeClick(){
  state.queryParams.fileClassCode=classTreeRef.value.selectNode.key;
}
//查询
function queryCollectData(){
    fileCollectDataTable.value.query(state.queryParams);
}

function addFileCollectData() {
    fileCollectDataPage.value.save().then(()=>{
        refreshTree();
    })
}

//刷新做成归集文件树
function refreshTree(){
    classTreeRef.value.refreshTree();
}


//查看详情
function queryDetail(){
    if(state.queryParams.fileClassCode===null||state.queryParams.fileClassCode===undefined||state.queryParams.fileClassCode===''){
        $messageBox.warning('请先选择归集目录', '提示');
        return false;
    }
    state.modalTitle = '文件归集参数详细信息';
    let params = {fileCollectCode:state.queryParams.fileClassCode,funFlag:'1'} 
    state.selectRow=params
    state.showFlag=true
    state.showFlagSave=true
    state.visible = true;
}

//子级新增
function childClassAdd(){
    if(state.queryParams.fileClassCode===null||state.queryParams.fileClassCode===undefined||state.queryParams.fileClassCode===''){
        $messageBox.warning('请先选择父级目录', '提示');
        return false;
    }
    state.modalTitle = '子级新增';
    let params = {fileCollectCode:state.queryParams.fileClassCode,funFlag:'2'} 
    state.selectRow=params
    state.showFlag=false;
    state.showFlagSave=false
    state.visible = true;
}
//根目录新增
function baseClassAdd(){
    state.modalTitle = '根目录新增';
    let params = {funFlag:'3'} 
    state.selectRow=params
    state.showFlag=false;
    state.showFlagSave=false
    state.visible = true;
}
//删除分类
function delClass(){
    if(state.queryParams.fileClassCode===null||state.queryParams.fileClassCode===undefined||state.queryParams.fileClassCode===''){
        $messageBox.warning('请先选择归集目录', '提示');
        return false;
    }
    state.modalTitle = '删除文件归集参数信息';
    let params = {fileCollectCode:state.queryParams.fileClassCode,funFlag:'4'} 
    state.selectRow=params
    state.showFlag=true
    state.showFlagSave=false
    state.visible = true;
}

//修改
function editDetail(){
    if(state.queryParams.fileClassCode===null||state.queryParams.fileClassCode===undefined||state.queryParams.fileClassCode===''){
        $messageBox.warning('请先选择归集目录', '提示');
        return false;
    }
    state.modalTitle = '修改文件归集参数信息';
    let params = {fileCollectCode:state.queryParams.fileClassCode,funFlag:'5'} 
    state.selectRow=params
    state.showFlag=false
    state.showFlagSave=false
    state.visible = true;
}

//查询文件元数据信息
function link(row){
  let info= {fileId:row.fileId};
  state.fileMetaDataVisible=true;
  nextTick(()=>{
    fileMetaDataRef.value.setInitData(info);
  })
}

//查询文件详细信息
function edit(row){
  state.fileInfoVisible=true;
  nextTick(() => {
    fileInfoRef.value.setInitCollectData(row);
  });
}
//修改文件信息
function saveFileInfo(){
  let updateInfo= fileInfoRef.value.fileInfo;
  $api.common.fileSend.post('fileCollectData0005',updateInfo).then((res) => {
    state.fileInfoVisible=false;
    $messageBox.success('文件信息保存成功', '提示');
    queryCollectData();
  }).catch(() => {
    $messageBox.error('文件信息保存失败', '提示');
    state.fileInfoVisible=false;
  });
}
//下载文件
function download(row){
  let showId=row.showId;
  //拼接文件地址
  let path = $api.common.fileDownloadUrl.url + showId;
  let a = document.createElement('a');
  a.href = path;
  a.click();
}


</script>
